/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.dao;

import com.wtkj.dao.impl.ListResultSetExtractor;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wtkj.vo.dzzf.base.kfwd.OBU;
import com.wtkj.vo.dzzf.base.kfzx.ObuGoods;
import com.wttech.dzzf.process.model.Organization;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 填充缺失数据
 *
 * @author MaShibo
 * @date 2017/12/11.
 */
@Repository
public class FillMissingDataDao {

	@Resource(name = "dzzfJdbcTemplate")
	private JdbcTemplate dzzfJdbcTemplate;

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate testJdbcTemplate;

	@SuppressWarnings("unchecked")
	public List<CardInfo> queryMissingInfoCards(int processNum) {
		final String sql =
				"SELECT *\n" +
						"  FROM (SELECT A.*, A.ROWID AS RNM, ROWNUM AS RM\n" +
						"          FROM AC_CARDINFO A\n" +
						"         WHERE A.ECARDSTATUS IN (1, 2, 3)\n" +
						"           AND (A.BINDACCOUNTTYPE IS NULL OR A.BINDCARDTYPE IS NULL OR\n" +
						"               A.CARDBRAND IS NULL))\n" +
						" WHERE RM <= ? ";
		return (List<CardInfo>) dzzfJdbcTemplate.query(sql, new ListResultSetExtractor(new CardInfo()), processNum);
	}

	@SuppressWarnings("unchecked")
	public List<OBU> queryMissingInfoObus(int processNum) {
		final String sql =
				"SELECT *\n" +
						"  FROM (SELECT M.*, K.ID AS GOODSID, ROWNUM AS RNM\n" +
						"          FROM (SELECT O.*,\n" +
						"                       ROW_NUMBER() OVER(PARTITION BY O.OBUNO ORDER BY O.OPTON DESC) AS RM\n" +
						"                  FROM AC_OBU O) M\n" +
						"          LEFT JOIN KC_OBUGOODS K\n" +
						"            ON M.OBUNO = K.OBUNO\n" +
						"         WHERE RM = 1\n" +
						"           AND (K.BRAND IS NULL OR K.MODEL IS NULL))\n" +
						" WHERE RNM <= ?";
		return (List<OBU>) dzzfJdbcTemplate.query(sql, new ListResultSetExtractor(new OBU()), processNum);
	}

	public int queryFundAccountIdNum(String fundAccountId){
		final String sql = "SELECT COUNT(1) FROM AC_FUNDACCOUNT A WHERE A.FUNDACCOUNTID = ? ";
		return dzzfJdbcTemplate.queryForObject(sql, Integer.class, fundAccountId);
	}

	public int queryTestFundAccountNum(){
		final String sql = "SELECT COUNT(1) FROM AC_FUNDACCOUNT ";
		return testJdbcTemplate.queryForObject(sql, Integer.class);
	}


	public List<Organization> queryTestOrgInfos(){
		final String sql = "SELECT * FROM JG_ORGANIZATION ";
		return testJdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Organization.class));
	}

	@SuppressWarnings("unchecked")
	public List<ObuGoods> queryObuGoodsByRow(){
		 final String sql =
				 "SELECT *\n" +
						 "  FROM (SELECT A.*, K.CONTRACTNO AS OBUID, ROWNUM AS RM\n" +
						 "          FROM KC_OBUGOODS A\n" +
						 "          LEFT JOIN KC_OBU K\n" +
						 "            ON A.OBUNO = K.OBUNO\n" +
						 "         WHERE A.BRAND IS NULL\n" +
						 "            OR A.MODEL IS NULL)\n" +
						 " WHERE RM >= 1\n" +
						 "   AND RM <= 10000";
		return (List<ObuGoods>) dzzfJdbcTemplate.query(sql, new ListResultSetExtractor(new ObuGoods()));
	}
}
